<?php

namespace App\Admin\Eloquent;
use App\Admin\Eloquent\User;
use App\Admin\Eloquent\GiftActivity;
use App\Exceptions\BusinessException;
use Router;

class GiftReceive extends Model
{
  public function __constract(){
    parent::__constract();
    //更新红包活动状态
    GiftActivity::updateActivity();
  }
  /**
   * 实例化模型时, 可批量赋值属性白名单
   *
   * @var array
   */
  protected $fillable = [
    'activity_id', 'activity_type','begin_time','end_time','user_id','input_time','use_time','money','status'
  ];
  public $timestamps = false;

  /**
   * 分页查询红包领取记录
   * @param  array       $options  [description]
   * @param  int|integer $pagesize [description]
   * @return [type]                [description]
   */
  public static function paging_query(array $options, int $pagesize = 15){
    $query = GiftReceive::join('gift_activities','gift_activities.id','=','gift_receives.activity_id')
      ->join('users','users.id','=','gift_receives.user_id')
    	->join('user_info','user_info.user_id','=','users.id')
    	->select('gift_receives.*','users.username','users.phone_number','gift_activities.name','user_info.name as uname');
    
    if(isset($options['activity_id']) && !empty($options['activity_id'])){
    	$query->where('gift_receives.activity_id','=',$options['activity_id']);
    }
    if(isset($options['uname']) && !empty($options['uname'])){
  		$query->where(function ($query) use ($options) {
        $query->orWhere('users.username','like','%'.$options['uname'].'%')
              ->orWhere('user_info.name', 'like', '%'.$options['uname'].'%')
              ->orWhere('users.phone_number', 'like', '%'.$options['uname'].'%');
      });
    }

    return $query->paginate($pagesize);
  }
}